(defun divide(m n)
  (floor (/ m n)))

(defun numbers (n)
  (loop for i = 1 then (1+ i)
     for j = 10 then (* 10 j)
     while (<= j n) 
     finally (return i)))

(defun round-up (n)
  (expt 10 (numbers n)))

(defun pad-with-zero (m n)
  (loop for i = m then (* 10 i)
       while (<= i n)
       finally (return i)))

;a / b = m + n
;a <- n
;l append m

(defun period (n)
  (let ((result nil))
    (loop for i from 1 to 125
       for a = (round-up n) then (mod (* 10 a) n)
       for d = (divide a n)
       do (format t "pasul ~d impart ~d la ~d si da ~d ~%" i a n d)
       ;when (find d result)
       ;return result
       ;do (format t "found period with ~d~%" d)
       when (not (zerop (mod a n)))
       do (setq result (cons d result))
       when (zerop (mod a n))
       do (return 0)
       when (= (mod a n) a)
       do (setq result (cons '0 result))
      ; when (and (not (= (mod n a) a)) (zerop (mod a 10)))
      ; do (loop for k = a then (floor (/ k 10))
;	     while (zerop (mod k 10))
;	     do (setq result (cons '0 result)))
       finally (return result))))


; 7 -> 0.(142857)
(defun period (n)
  (let ((as ())
	(bs()))
    (loop for i from 1 to 1000
	 for a = 1 then (mod (* 10 a) n)
	 for b = (floor (/ (* a 10) n))
;do (format t "(~d)a=~d b=~d~%" i a b)
	 when (find a as) do 
	 (return (position a as))
;(format t "found ~d in list size ~d started at ~d~%" a (length as) (position a as))
;(return  (- (length as) (position a as)))
	 do (setq bs (cons b bs) as (cons a as))
	 finally (return as))))

(defun probl26()
  (loop 
     for i from 2 to 1000 
     with max = 0 
     with maxi = 0 
     when (< max (period i)) do (setq max (period i) maxi i)
     finally (return maxi))))